/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlers.Admin;

import Pojos.NhomThanhVien;
import Pojos.ThanhVien;
import Util.MyUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ToanNhan
 */
@WebServlet(name = "CapNhatThongTinThanhVienControler", urlPatterns = {"/CapNhatThongTinThanhVienControler"})
public class CapNhatThongTinThanhVienControler extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            String command = request.getParameter("command");
            if (command == null || command.length() == 0) {
                response.sendRedirect("/WebsiteBanHang/Admin/CapNhapThongTinThanhVien.jsp");
            } else {
                command = command.toLowerCase();
                if (command.compareTo("laythongtinthanhvien") == 0) {
                    System.out.println("-------------------------Lay thong tin thanh vien--------------");
                    layThongTinThanhVien(request, response);
                    
                }
                if (command.compareTo("laydanhsachnhomthanhvien") == 0) {
                    System.out.println("-------------------------Lay danh sach nhom thanh vien--------------");
                    layDanhSachNhomThanhVien(request, response);
                    
                }
            }
            command = request.getParameter("btnLuu");
            if (command != null) {
                capNhatThongTinThanhVien(request, response);
            }

        } finally {
        }
    }

    protected void layThongTinThanhVien(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        if (userName != null) {
            SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
            Session session = factory.openSession();
            Query query = session.createQuery("From ThanhVien Where userName = ?");
            query.setString(0, userName);
            ThanhVien tv = (ThanhVien)query.uniqueResult();
            request.setAttribute("thanhVien", tv);
            session.close();
            request.getRequestDispatcher("Admin/CapNhatThongTinThanhVien.jsp").forward(request, response);
        }
    }

    protected void layDanhSachNhomThanhVien(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
        Session session = factory.openSession();
        Query query = session.createQuery("From NhomThanhVien");
        List<NhomThanhVien> lstNhomThanhVien = query.list();
        session.close();
        request.setAttribute("nhomThanhVien", lstNhomThanhVien);
        request.getRequestDispatcher("Admin/CapNhatThongTinThanhVien.jsp").forward(request, response);
    }

    protected void capNhatThongTinThanhVien(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String password = request.getParameter("password");
        String maNhomThanhVien = request.getParameter("nhomThanhVien");
        String userName = request.getParameter("userName");
        SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
        Session session = factory.openSession();
        session.beginTransaction();
        Query query = session.createQuery("From ThanhVien Where userName = ?");
        query.setString(0, userName);
        ThanhVien tv = (ThanhVien)(query.uniqueResult());
        if (tv != null){
            tv.setNhomThanhVien(new NhomThanhVien(maNhomThanhVien,""));
        if (password != null && password.length() != 0){
            password = MyUtil.maHoaMD5(password);
            tv.setPassword(password);
        }
            session.update(tv);
            session.getTransaction().commit();
        }
        session.close();
        response.sendRedirect("/WebsiteBanHang/Admin/XemDanhSachThanhVien.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
